/**
 * 响应拦截
 * @param {Object} http 
 */
module.exports = (vm) => {
	const rememberMeHeader = 'x-remember';
    uni.$u.http.interceptors.response.use( async (res) => { /* 对响应成功做点什么 可使用async await 做异步操作*/
       console.log('response', res);
       let data = res.data;
       if (!(data)){
       	vm.$u.toast('未连接到服务器')
       	return false;
       }
       if (typeof data === 'object' && !(data instanceof Array)){
       	if (data.token){
       		vm.$u.vuex('vuex_token', data.token);
       		if (data.user){
       			vm.$u.vuex('vuex_user', data.user);
       		}
       		vm.$u.vuex('vuex_isAgent', data.isAgent);
       	}
       	if (data.result === 'login'){
       		vm.$u.vuex('vuex_user', {});
       		if (req.tryagain == undefined || req.tryagain){
       			req.tryagain = false; req.remember = true;
       			await vm.$u.http.request(req).then(res => {
       				data = res;
       			});
       		}
       		if (data.result === 'login'){
       			if (!req.data.loginCheck){
       				vm.$u.toast(data.msg);
       			}
       			req.tryagain = true;
       		}
       	}
       }
       
       if (res.header && res.header[rememberMeHeader]){
       	let remember = res.header[rememberMeHeader];
       	if (remember && remember != 'deleteMe'){
       		vm.$u.vuex('vuex_remember', remember);
       	}else{
       		vm.$u.vuex('vuex_remember', '');
       	}
       }
       return data;
    })
}